<?php
namespace Admin\Controller;

class ServerStatsController extends \Think\Controller {

    public function index() {
        $m    = D2('Server');
        $page = $m->order('id DESC')->page2();

        $this->assign('page', $page)->display();
    }

    public function index2() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            exit('没有找到服务器数据');
        }

        $ifstatsX   = range(0, 59);
        $loadStatsX = $ifstatsX;
        $ifstats    = array();
        $loadStats  = array();
        $defaultVal = array_fill(0, 60, 0);
        $mIfstats   = D2('Ifstats');
        $mLoad      = D2('Load');

        foreach($server as $v) {
            $ifstatsInKey  = $v['ip'] . '-IN';
            $ifstatsOutKey = $v['ip'] . '-OUT';
            $loadStatsKey  = $v['ip'];

            if(empty($ifstats[$ifstatsInKey])) {
                $ifstats[$ifstatsInKey] =  array(
                    'name' => $v['ip'] . ' IN',
                    'data' => $defaultVal,
                );
            }

            if(empty($ifstats[$ifstatsOutKey])) {
                $ifstats[$ifstatsOutKey] = array(
                    'name' => $v['ip'] . ' OUT',
                    'data' => $defaultVal,
                );
            }

            if(empty($loadStats[$loadStatsKey])) {
                $loadStats[$loadStatsKey] = array(
                    'name' => $v['ip'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $mIfstats->getHourStatsByServerId($v['id'])) {
                continue;
            }

            if(!empty($tmp['in'])) {
                $ifstats[$ifstatsInKey]['data'] = $tmp['in'];
            }

            if(!empty($tmp['out'])) {
                $ifstats[$ifstatsOutKey]['data'] = $tmp['out'];
            }

            if(!$tmp = $mLoad->getHourStatsByServerId($v['id'])) {
                continue;
            }

            $loadStats[$loadStatsKey]['data'] = $tmp;
        }

        $this->assign('ifstats',   array_values($ifstats))->assign('ifstatsX',     $ifstatsX);
        $this->assign('loadStats', array_values($loadStats))->assign('loadStatsX', $loadStatsX);
        $this->display();
    }

    /**
     * apiIfStatsHour
     * 获取当前小时的流量数据
     *
     * @return void
     */
    public function apiIfStatsHour() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $ifstats    = array();
        $defaultVal = array_fill(0, 60, 0);
        $mIfstats   = D2('Ifstats');

        foreach($server as $v) {
            $ifstatsOutKey = $v['id'] . '-OUT';

            if(empty($ifstats[$ifstatsOutKey])) {
                $ifstats[$ifstatsOutKey] = array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $mIfstats->getHourStatsByServerId($v['id'])) {
                continue;
            }

            if(!empty($tmp['out'])) {
                $ifstats[$ifstatsOutKey]['data'] = $tmp['out'];
            }
        }

        $r = array(
            'x' => range(0, 59),
            'y' => array_values($ifstats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiIfStatsDay
     * 获取当天的流量数据
     *
     * @return void
     */
    public function apiIfStatsDay() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $ifstats    = array();
        $defaultVal = array_fill(0, 24, 0);
        $mIfstats   = D2('Ifstats');

        foreach($server as $v) {
            $ifstatsOutKey = $v['id'] . '-OUT';

            if(empty($ifstats[$ifstatsOutKey])) {
                $ifstats[$ifstatsOutKey] = array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $mIfstats->getDayStatsByServerId($v['id'])) {
                continue;
            }

            if(!empty($tmp['out'])) {
                $ifstats[$ifstatsOutKey]['data'] = $tmp['out'];
            }
        }

        $r = array(
            'x' => range(0, 59),
            'y' => array_values($ifstats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiIfStatsMonth
     * 获取当月的流量数据
     *
     * @return void
     */
    public function apiIfStatsMonth() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $ifstats    = array();
        $defaultVal = array_fill(0, date('t'), 0);
        $mIfstats   = D2('Ifstats');

        foreach($server as $v) {
            $ifstatsOutKey = $v['id'] . '-OUT';

            if(empty($ifstats[$ifstatsOutKey])) {
                $ifstats[$ifstatsOutKey] = array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $mIfstats->getMonthStatsByServerId($v['id'])) {
                continue;
            }

            if(!empty($tmp['out'])) {
                $ifstats[$ifstatsOutKey]['data'] = $tmp['out'];
            }
        }

        $r = array(
            'x' => range(1, date('t')),
            'y' => array_values($ifstats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiIfStatsYear
     * 获取当年的流量数据
     *
     * @return void
     */
    public function apiIfStatsYear() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $ifstats    = array();
        $defaultVal = array_fill(0, 12, 0);
        $mIfstats   = D2('Ifstats');

        foreach($server as $v) {
            $ifstatsOutKey = $v['id'] . '-OUT';

            if(empty($ifstats[$ifstatsOutKey])) {
                $ifstats[$ifstatsOutKey] = array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $mIfstats->getYearStatsByServerId($v['id'])) {
                continue;
            }

            if(!empty($tmp['out'])) {
                $ifstats[$ifstatsOutKey]['data'] = $tmp['out'];
            }
        }

        $r = array(
            'x' => range(1, 12),
            'y' => array_values($ifstats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiLoadStatsHour
     * 获取当前小时负载数据
     *
     * @return void
     */
    public function apiLoadStatsHour() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJson(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $loadStats  = array();
        $defaultVal = array_fill(0, 60, 0);
        $mLoad      = D2('Load');

        foreach($server as $v) {
            $loadStatsKey  = $v['id'];

            if(empty($loadStats[$loadStatsKey])) {
                $loadStats[$loadStatsKey] = array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $mLoad->getHourStatsByServerId($v['id'])) {
                continue;
            }

            $loadStats[$loadStatsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(0, 59),
            'y' => array_values($loadStats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiLoadStatsDay
     * 获取当天的负载
     *
     * @return void
     */
    public function apiLoadStatsDay() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $loadStats  = array();
        $defaultVal = array_fill(0, 24, 0);
        $m          = D2('Load');

        foreach($server as $v) {
            $loadStatsKey  = $v['id'];

            if(empty($loadStats[$loadStatsKey])) {
                $loadStats[$loadStatsKey] =  array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $m->getDayStatsByServerId($v['id'])) {
                continue;
            }

            $loadStats[$loadStatsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(0, 24),
            'y' => array_values($loadStats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiLoadStatsMonth
     * 获取当月的负载
     *
     * @return void
     */
    public function apiLoadStatsMonth() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $loadStats  = array();
        $defaultVal = array_fill(0, date('t'), 0);
        $m          = D2('Load');

        foreach($server as $v) {
            $loadStatsKey  = $v['id'];

            if(empty($loadStats[$loadStatsKey])) {
                $loadStats[$loadStatsKey] =  array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $m->getMonthStatsByServerId($v['id'])) {
                continue;
            }

            $loadStats[$loadStatsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(1, date('t')),
            'y' => array_values($loadStats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiLoadStatsYear
     * 获取当年的负载
     *
     * @return void
     */
    public function apiLoadStatsYear() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $loadStats  = array();
        $defaultVal = array_fill(0, 12, 0);
        $m          = D2('Load');

        foreach($server as $v) {
            $loadStatsKey  = $v['id'];

            if(empty($loadStats[$loadStatsKey])) {
                $loadStats[$loadStatsKey] =  array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $m->getYearStatsByServerId($v['id'])) {
                continue;
            }

            $loadStats[$loadStatsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(1, 12),
            'y' => array_values($loadStats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiNginxStatsHour
     * 获取当前小时nginx连接数
     *
     * @return void
     */
    public function apiNginxStatsHour() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJson(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $stats      = array();
        $defaultVal = array_fill(0, 60, 0);
        $m          = D2('Nginx');

        foreach($server as $v) {
            $statsKey  = $v['id'];

            if(empty($stats[$statsKey])) {
                $stats[$statsKey] = array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $m->getHourStatsByServerId($v['id'])) {
                continue;
            }

            $stats[$statsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(0, 59),
            'y' => array_values($stats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiNginxStatsDay
     * 获取当天的nginx连接数
     *
     * @return void
     */
    public function apiNginxStatsDay() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $stats      = array();
        $defaultVal = array_fill(0, 24, 0);
        $m          = D2('Nginx');

        foreach($server as $v) {
            $statsKey  = $v['id'];

            if(empty($stats[$statsKey])) {
                $stats[$statsKey] =  array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $m->getDayStatsByServerId($v['id'])) {
                continue;
            }

            $stats[$statsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(0, 24),
            'y' => array_values($stats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiNginxStatsMonth
     * 获取当月的nginx连接数
     *
     * @return void
     */
    public function apiNginxStatsMonth() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $stats      = array();
        $defaultVal = array_fill(0, date('t'), 0);
        $m          = D2('Nginx');

        foreach($server as $v) {
            $statsKey  = $v['id'];

            if(empty($stats[$statsKey])) {
                $stats[$statsKey] =  array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $m->getMonthStatsByServerId($v['id'])) {
                continue;
            }

            $stats[$statsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(1, date('t')),
            'y' => array_values($stats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * apiNginxStatsYear
     * 获取当年的nginx连接数
     *
     * @return void
     */
    public function apiNginxStatsYear() {
        $mServer = D2('Server');

        if(!$server = $mServer->order('id ASC')->select()) {
            showJsonError(array('s' => false, 'm' => '没有找到服务器数据'));
        }

        $stats      = array();
        $defaultVal = array_fill(0, 12, 0);
        $m          = D2('Nginx');

        foreach($server as $v) {
            $statsKey  = $v['id'];

            if(empty($stats[$statsKey])) {
                $stats[$statsKey] =  array(
                    'name' => $v['name'],
                    'data' => $defaultVal,
                );
            }

            if(!$tmp = $m->getYearStatsByServerId($v['id'])) {
                continue;
            }

            $stats[$statsKey]['data'] = $tmp;
        }

        $r = array(
            'x' => range(1, 12),
            'y' => array_values($stats),
        );

        showJson(array('s' => true, 'd' => $r));
    }

    public function modify() {
        $id = I('get.id', 0, 'intval');

        if(empty($id)) {
            halt('参数不完整');
        }

        $record = D2('Server')->getById($id);

        $this->assign('record', $record)->display();
    }

    public function add() {
        $this->display();
    }

    public function save() {
        $d = $_POST;
        $m = D2('Server');

        try {
            $r = $m->save2($d)
                 ? array('s' => true, 'm' => '数据保存成功')
                 : array('s' => false, 'm' => '数据保存出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

    public function del() {
        $id = I('get.id', 0, 'intval');

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整'));
        }

        $m = D2('Server');

        try {
            $r = $m->del($id)
                 ? array('s' => true, 'm' => '删除成功')
                 : array('s' => false, 'm' => '删除出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

}
